<?php
/**
 * CodeModel
 * 
 * @author Admin
 * @version 
 */
require_once 'Zend/Db/Table/Abstract.php';
class Admin_Models_LoginLogModel extends Zend_Db_Table_Abstract
{
    /**
     * The default table name 
     */
    protected $_name = 'login_log';
    protected $_primary = 'id';
    
	function countTotalLogByDay(){
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		
		$mySql = $db->select()
					->from(array('u'	=> $this->_name),array('log'	=> 'count(*)','day'	=> 'unix_timestamp(Date(u.time))'))
					->group('day')
					->order('day ASC');
		$result = $db->fetchAll($mySql);
		return $result;
	}
	function countTotalUserLoginByDay(){
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$pre_sql = $db->select()
					->from(array('u'	=> $this->_name),array('UserID','day'	=> 'unix_timestamp(Date(u.time))'))
					->distinct();
		$mySql = $db->select()
					->from(array('u'	=> new Zend_Db_Expr("(" . $pre_sql . ")")),array('log'	=> 'count(*)','day'))
					->group('day')
					->order('day ASC');
		
		$result = $db->fetchAll($mySql);
		return $result;
	}
	function countTotalUserLoginByDayByData($data){
	    if($data['time_search']=='0'){
	    	$dk3= "u.time BETWEEN '".$data['Starttime']."' AND '".$data['Endtime']."'"  ;
	    }else if($data['time_search']=='1'){
	    	$dk3= "u.time BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'"  ;
	    }else if($data['time_search']=='2'){
	    	$data_week = $this->rangeWeek(date('Y-m-d'));
	    	$dk3= "u.time BETWEEN '".$data_week['start']."' AND '".$data_week['end']."'"  ;
	    }else if($data['time_search']=='3'){
	    	$data_month = $this->rangeMonth(date('Y-m-d'));
	    	$dk3= "u.time BETWEEN '".$data_month['start']."' AND '".$data_month['end']."'"  ;
	    }else{
	    	$dk3="";
	    }
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$pre_sql = $db->select()
		->from(array('u'	=> $this->_name),array('UserID','day'	=> 'unix_timestamp(Date(u.time))'))
		->distinct();
		if($data['ProductID']!="0") $pre_sql->join(array('up'=>'userproduct'), 'u.UserId = up.UserID',array('ProductID'))
		->where('up.ProductID = '.$data['ProductID']);
		if($dk3!="") $pre_sql->where($dk3);
		$mySql = $db->select()
		->from(array('u'	=> new Zend_Db_Expr("(" . $pre_sql . ")")),array('log'	=> 'count(*)','day'))
		->group('day')
		->order('day ASC');

		$result = $db->fetchAll($mySql);
		return $result;
	}
	function rangeMonth($datestr) {
		date_default_timezone_set(date_default_timezone_get());
		$dt = strtotime($datestr);
		$res['start'] = date('Y-m-d', strtotime('first day of this month', $dt));
		$res['end'] = date('Y-m-d', strtotime('last day of this month', $dt));
		return $res;
	}
	
	function rangeWeek($datestr) {
		date_default_timezone_set(date_default_timezone_get());
		$dt = strtotime($datestr);
		$res['start'] = date('N', $dt)==1 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('last monday', $dt));
		$res['end'] = date('N', $dt)==7 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('next sunday', $dt));
		return $res;
	}
}